<template>
  <div style="margin-top: 20px">
    <div class="my-btn" >
      <a-button type="primary" @click="saveHistory">
        存入历史
      </a-button>
    </div>
    <!--表格开始-->
    <a-table :columns="columns"
             rowKey="id"
             :expandedRowKeys="expandedRowKeys"
             :data-source="workloads"
             class="components-table-demo-nested"
             @expand="expand"
             :pagination="false"
             :bordered="true">

      <template slot="teacher" slot-scope="record">
        <span v-text="teacherStr(record.teacherId)"></span>
      </template>
      <a-table
          slot="expandedRowRender"
          :columns="innerColumns"
          :data-source="tasks"
          :pagination="false"
          id="innerTable"
          :bordered="true">

        <template slot="course" slot-scope="record">
          <span v-text="courseStr(record.plan.program.courseId)"></span>
        </template>
        <template slot="courseType" slot-scope="record">
          <span v-text="courseTypeStr(record.plan.program.courseId)"></span>
        </template>
        <template slot="grade" slot-scope="record">
          <span v-text="gradeStr(record.plan.program.gradeId)"></span>
        </template>
        <template slot="major" slot-scope="record">
          <span v-text="majorStr(record.plan.program.majorId)"></span>
        </template>
        <template slot="merge" slot-scope="record">
          <span v-text="record.merge == 0 ? '否' : '是'"></span>
        </template>
        <template slot="semester" slot-scope="record">
          <span v-text="semesterStr(record.plan.semester)"></span>
        </template>
        <template slot="operation" slot-scope="text, record">
          <div class="editable-row-operations">
            <a-popconfirm title="确认删除?" @confirm="() => delTask(record.id)">
              <a>删除</a>
            </a-popconfirm>
          </div>
        </template>
      </a-table>
    </a-table>
    <div class="pagination">
      <a-pagination
          v-model="pageParam.current"
          show-size-changer
          :page-size.sync="pageParam.pageSize"
          :total="pageParam.total"
          @showSizeChange="onShowSizeChange"
          @change="onPageChange"
      />
    </div>
  </div>
</template>
<script>

import {totalCourse} from "@/services/course";
import {totalCourseType} from "@/services/courseType";
import {totalUser} from "@/services/user";
import {totalGrade} from "@/services/grade";
import {totalMajor} from "@/services/major";
import {listTaskWorkloadVOByPage, listTaskVOByTeacherId, deleteTask} from "@/services/task";
import {saveTaskFinal} from "@/services/taskFinal";

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    scopedSlots: { customRender: 'id' },
  },
  {
    title: '授课教师',
    key: 'teacherId',
    scopedSlots: { customRender: 'teacher' },
  },
  {
    title: '工作量',
    dataIndex: 'workload',
    scopedSlots: { customRender: 'workload' },
  }
]

const innerColumns = [
  // {
  //   title: 'id',
  //   dataIndex: 'id',
  //   scopedSlots: { customRender: 'id' },
  // },
  {
    title: '课程',
    key: 'plan.program.courseId',
    scopedSlots: { customRender: 'course' },
  },
  {
    title: '课程类型',
    width: '12%',
    key: 'plan.program.course',
    scopedSlots: { customRender: 'courseType' },
  },
  {
    title: '年级',
    key: 'plan.program.gradeId',
    scopedSlots: { customRender: 'grade' },
  },
  {
    title: '专业',
    key: 'plan.program.majorId',
    scopedSlots: { customRender: 'major' },
  },
  {
    title: '学分',
    dataIndex: 'plan.program.credit',
    scopedSlots: { customRender: 'credit' },
  },
  {
    title: '理论学时',
    dataIndex: 'plan.program.theoretical',
    scopedSlots: { customRender: 'theoretical' },
  },
  {
    title: '实验学时',
    dataIndex: 'plan.program.experimental',
    scopedSlots: { customRender: 'experimental' },
  },
  {
    title: '教学班人数',
    dataIndex: 'actualNum',
    scopedSlots: { customRender: 'actualNum' },
  },
  {
    title: '是否合班',
    key: 'merge',
    scopedSlots: { customRender: 'merge' },
  },
  {
    title: '学年学期',
    key: 'plan.semester',
    scopedSlots: { customRender: 'semester' },
  },
  {
    title: '操作',
    dataIndex: 'operation',
    scopedSlots: { customRender: 'operation' },
  },
]

export default {
  components: {  },
  data() {
    return {
      pageParam :{
        pageSize: 10,
        current: 1,
        total: 200,
      },
      workloads: [],
      tasks: [],
      columns,
      innerColumns,
      expandedRowKeys: [],//控制显示子表格
      teachers: [],
      courses: [],
      courseTypes: [],
      grades: [],
      majors: []

    };
  },

  methods: {
    delTask(id) {
      deleteTask(id).then(res => {
        if (res.data.code == 200){
          this.$success = "删除成功"
          this.expandedRowKeys = []
          this.refreshData()
        } else {
          this.$error = '删除失败'
        }
      })
    },
    refreshData() {
      this.onPageChange(this.pageParam.current, this.pageParam.pageSize )
    },

    onShowSizeChange(current, pageSize) {
      listTaskWorkloadVOByPage(current, pageSize).then(this.afterChange)
    },
    onPageChange(page, pageSize) {
      listTaskWorkloadVOByPage(page, pageSize).then(this.afterChange)
    },
    afterChange(res) {
      const changeRes = res.data
      if (changeRes.code == 200){
        const pageRes = changeRes.data
        this.pageParam.current = pageRes.current
        this.pageParam.pageSize = pageRes.size
        this.pageParam.total = pageRes.total
        this.workloads = pageRes.records
      } else {
        this.$message.error = changeRes.message
      }
    },
    listTotalCourse() {
      totalCourse().then(res => {
        const result = res.data
        if (result.code == 200) {
          this.courses = result.data
        } else {
          this.$message.error(result.message)
        }
      })
    },
    listTotalCourseType() {
      totalCourseType().then(res => {
        const result = res.data
        if (result.code == 200) {
          this.courseTypes = result.data
        } else {
          this.$message.error(result.message)
        }
      })
    },
    listTotalGrade() {
      totalGrade().then(res => {
        const result = res.data
        if (result.code == 200) {
          this.grades = result.data
        } else {
          this.$message.error(result.message)
        }
      })
    },
    listTotalMajor() {
      totalMajor().then(res => {
        const result = res.data
        if (result.code == 200) {
          this.majors = result.data
        } else {
          this.$message.error(result.message)
        }
      })
    },
    listTotalTeacher() {
      totalUser().then(res => {
        const result = res.data
        if (result.code == 200) {
          this.teachers = result.data
        } else {
          this.$message.error(result.message)
        }
      })
    },
    teacherStr(id) {
      const teacher = this.teachers.find(item => item.id === id);
      return teacher.username
    },
    courseStr(id) {
      const course = this.courses.find(item => item.id === id);
      return course.name
    },
    courseTypeStr(id) {
      const course = this.courses.find(item => item.id === id);
      console.log(course)
      const courseType = this.courseTypes.find(item => item.id === course.typeId);
      return courseType.name
    },
    gradeStr(id) {
      const grade = this.grades.find(item => item.id === id);
      return grade.grade
    },
    majorStr(id) {
      const major = this.majors.find(item => item.id === id);
      return major.name
    },
    semesterStr(semester) {
      const now = semester.year
      const next = now + 1
      const num = semester.number
      return now + '-' + next + '-' + num
    },

    saveHistory() {
      //将学期教学任务手动存入历史， 仅作演示，实际采用定时任务处理
      saveTaskFinal().then(res => {
        const result = res.data
        if (result.code == 200) {
          this.$message.success('存入成功')
        } else {
          this.$message.error(result.message)
        }
      })
    },

    //展开节点操作
    expand(expanded, record) {
      this.expandedRowKeys = []
      if (expanded) {
        if (record){
          this.expandedRowKeys = [record.id];
        }
        listTaskVOByTeacherId(record.teacherId).then(res => {
          const result = res.data
          if (result.code == 200) {
            this.tasks = result.data
          } else {
            this.$message.error(result.message)
          }
        })
      }
    },
  },
  created() {
    this.refreshData()
    this.listTotalTeacher()
    this.listTotalCourse()
    this.listTotalCourseType()
    this.listTotalGrade()
    this.listTotalMajor()
  }
};
</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}

.my-btn {
  text-align: right;
  margin-right: 10px;
  margin-bottom: 10px;
}

</style>
